<template>
  <div class="login-body">
    <div class="login-panel">
      <transition-group appear
                        name="animate__animated animate__bounce"
                        enter-active-class="animate__backInLeft"
                        leave-active-class="animate__backOutRight">
        <div class="login-title">用户{{computedFormTitle}}</div>
        <el-form :model="formData" :rules="rules" ref="formDataRef">
          <el-form-item prop="username">
            <el-input placeholder="请输入账号" v-model="formData.username" size="large" type="text">
              <template #prefix>
                <el-icon>
                  <User />
                </el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input placeholder="请输入密码" v-model="formData.password" size="large" type="password"
                      @keyup.enter="submit()">
              <template #prefix>
                <el-icon>
                  <Lock />
                </el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item label="">
            <div class="check-code-panel flex-row-between width-100-percent">
              <el-input placeholder="请输入验证码" v-model="formData.validate" class="input-panel" />
              <img  @click="getCaptcha" :src="validateSrc.validCodeBase64" class="check-code clickable margin-row-40">
            </div>
          </el-form-item>
          <el-form-item label="">
            <div class="flex-row-between width-80-percent">
              <el-checkbox label="记住密码" name="type" />
              <el-button text  @click="showLoginForm=!showLoginForm">{{showLoginForm?'注册':'登录'}}</el-button>

            </div>

          </el-form-item>
          <el-form-item label="">
            <el-button :loading="showBtnLoading"  type="primary" style="width: 100%;" @click="submit" size="large">{{computedFormTitle}}</el-button>
          </el-form-item>
        </el-form>
      </transition-group>
    </div>
  </div>
</template>

<script setup>
import {ref, reactive, computed,} from 'vue'
import {login, register, getPicCaptcha} from "@/api/user/UserApi"
import { ElMessage } from 'element-plus';
import router from "@/router";
import {setToken} from "@/utils/TokenUtil";
import useStore from "@/store/userStore"
import {User} from "@element-plus/icons";

// const checkCodeUrl = "api/checkCode?" + new Date().getTime();
//表单

let showLoginForm = ref(true)
const formDataRef = ref();
let formData = reactive({
  username: "",
  password: "",
  validate:""
});
let validateSrc=ref({
  validCodeBase64:"",
  validCodeReqNo:""
})

let showBtnLoading=ref(false)
const rules = {
  username: [{
    required: true,
    message: "请输入用户名"
  }],
  password: [{
    required: true,
    message: "请输入密码"
  }],
  // checkCode: [{
  //     required: true,
  //     message: "请输入验证码"
  // }],
}

getCaptcha()
function getUser(){
  let useUserStore = useStore()
  useUserStore.getUser()
}
function getCaptcha(){
  getPicCaptcha().then(resp=>{
    if(resp.code === 200){
      validateSrc.value={
        ...resp.data
      }
    }
  })
}

function loginUser(){
  showBtnLoading.value=true
  login(formData.username,formData.password).then(resp=>{
    if(resp.code === 200){
      ElMessage({
        type:"success",
        message:"登录成功"
      })

      // 存储token
      setToken(resp.data)
      // 查找用户信息存入store
      getUser()
      showBtnLoading.value=false
      setTimeout(()=>{
        // 返回上一页 TODO 根据路由参数redirect跳转
        router.go(-1)
      },1000)

    }
    showBtnLoading.value=false
  })
}

function registerUser(){
  showBtnLoading.value=true
  register(formData.username,formData.password).then(resp=>{
    if(resp.code === 200){
      ElMessage({
        type:"success",
        message:"注册成功"
      })
      loginUser()
    }
    showBtnLoading.value=false
  })

}
function submit(){
  formDataRef.value.validate((valid)=>{
    if(valid){
      showLoginForm.value?loginUser():registerUser()
    }
  })

}


let computedFormTitle = computed(()=>{
  return showLoginForm.value?'登录':'注册'
})
</script>

<style lang="less" scoped >
.login-body {
  //background: url("../assets/三门峡.png") no-repeat center center;
  height: 100%;
  width: 100%;
  background-size: cover;
  position: absolute;
  left: 0;
  top: 0;

  .login-panel {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    margin: auto;

    padding: 25px;
    width: 26%;
    min-width: 460px;
    height: 30%;
    min-height: 300px;
    background: rgba(255, 255, 255, 0.6);
    border-radius: 5%;
    box-shadow: 2px 2px 10px #ddd;

    .login-title {
      font-size: 22px;
      text-align: center;
      margin-bottom: 22px;
    }
  }
}
</style>
